//==================================================================
/// horizon.cpp
///
/// Created by Davide Pasca - 2009/10/5
/// See the file "license.txt" that comes with this project for
/// copyright info. 
//==================================================================

#include "stdafx.h"
#include "horizont.h"

//==================================================================
horizon::horizon( int w, int h )
{
	_y = h/2;
	_w = w;
	_h = h;
	colors_set(
		color( 10, 20, 220 ),
		color( 200, 200, 220 ),
		color( 200, 100, 10 ) );
}

//==================================================================
void horizon::Resize( U32 w, U32 h )
{
	_y = (int)h / 2;
	_w = (int)w;
	_h = (int)h;
}

//==================================================================
void horizon::to15bits( image &img )
{
	int	col, r, g, b, a;
	int	er, eg, eb;
	float	t;

	int w = img.mW;
	int h = img.mH;

	int i = 0;
	for (int y=0; y < h; ++y)
		for (int x=0; x < w; ++x, ++i)
		{
			col = img.GetPInt()[i];
			a = (col & 0xff000000) >> 24;
			r = (col & 0x00ff0000) >> 16;
			g = (col & 0x0000ff00) >>  8;
			b = (col & 0x000000ff) >>  0;

			er = r & 7; r &= ~7;
			eg = g & 7; g &= ~7;
			eb = b & 7; b &= ~7;

			t = (float)Math::Random()*7.0f;
			if ( er <= t )  er = 0; else er = 8; 

			//t = (float)Math::Random()*3.5f;
			if ( eg <= t )  eg = 0; else eg = 8; 

			//t = (float)Math::Random()*3.5f;
			if ( eb <= t )  eb = 0; else eb = 8; 

			//t = er * (float)Math::Random() / 8;
			r = r + er;
			g = g + eg;
			b = b + eb;

			//r &= ~7;
			//g &= ~7;
			//b &= ~7;
			img.GetPInt()[i] = (a << 24) | (r << 16) | (g << 8) | b;
		}
}

//==================================================================
void horizon::paint( graphics &g )
{
	int	x, y, w, h;

	if ( true )//mpoSkySprt != NULL )
	{
		x = (int)_x;
		w = mpoSkySprt->width_get( 0 );

		if NOT( w )
			return;

		if ( (U32)_y >= mpoSkySprt->mpoImages[0]->mH )
			y = mpoSkySprt->mpoImages[0]->mH-1;
		else
			y = _y;

		x %= w;
		if ( x + w < _w )
			mpoSkySprt->draw( g, 0, x+w, y, 1, 1 );
		else
			if ( x > 0 )
				mpoSkySprt->draw( g, 0, x-w, y, 1, 1 );

		mpoSkySprt->draw( g, 0, x, y, 1, 1 );

		//h = _y - mpoSkySprt->mpoImages[0]->mH;
		//if ( h > 0 )
		//	g.rect_fill( 0, 0, _w, h, _top1_col );

		//g.drawImage( mpoSkySprt->, 0, _y );
	}
	else
	{

		//g.setColor( _top1_col );
		//g.fillRect( 0, 0, _w, _y );

		for (x=0; x < _w; x += 64)
			mpoSkySprt->draw( g, 0, x, _y, 1.0f, sprite::NT_FLG );
		//g.image_draw( mpoSkySprt, x, _y - mH/2 );

		h = _y - mpoSkySprt->mpoImages[0]->mH;
		if ( h > 0 )
			g.rect_fill( 0, 0, _w, h, _top1_col );
	}

	//g.rect_fill( 0, _y, mW, _h, _bot_col );
}

//==================================================================
void horizon::create_sky( const color &col1, const color &col2 )
{
	int h = _h*2/3;

	graphics	gfx( 64, h );

	//r1 = b1 = g1;
	//r2 = b2 = g2;

	float max = (float)h-1;
	max = max/4 + max*max;
	for (int y=h-1; y >= 0; --y)
	{
		//coe = (float)y / h;
		float coe = (y/4 + y * y) / max;

		color newCol = color::make_inpt( col1, col2, coe );

		gfx.line_h2( 0, 64, y, newCol );
	}

	to15bits( *gfx.mpoImg.get() );

	mpoSkySprt = new sprite( gfx.mpoImg.get(), sprite::LEFT, sprite::BOTTOM );
	mpoSkySprt->shadow_off();
}

//==================================================================
void horizon::colors_set( const color &top1, const color &top2, const color &bot )
{
	_top1_col = top1;
	_bot_col = bot;

	create_sky( top1, top2 );
}

//==================================================================
void horizon::sky_picture_set( const DStr &picname )
{
	mpoSkySprt = new sprite( picname, sprite::LEFT, sprite::BOTTOM );
	mpoSkySprt->shadow_off();
}
